package com.dean.ui.panel.impl;

import com.dean.constants.DeanUiConstants;
import com.dean.ui.panel.DeanPanel;
import com.dean.ui.utils.UiUtils;
import com.dean.utils.FreemarkerUtils;
import com.dean.utils.PropertyUtils;
import com.formdev.flatlaf.extras.components.FlatSplitPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * dean 聊天框
 */
public class DeanChatPanel extends BasePanel implements DeanPanel {

    private static final Logger logger = LoggerFactory.getLogger(DeanChatPanel.class);
    /**
     * 配置 文件路径
     */
    private final String INIT_PATH_NAME = DeanUiConstants.INIT_PATH + "deanchat.properties";
    /**
     * 模板组件
     */
    private List<JCheckBox> ftls;
    /**
     * 选中模板
     */
    private List<String> ftlNames = new ArrayList<String>(16);

    /**
     * 左上布局 左 上 1 文件导出目录
     */
    private JLabel serverPortLable;
    private JTextField serverPortInput;
    /**
     * 左上布局 左 上 2 包名
     */
    private JLabel packageNameLabel;
    private JTextField packageNameInput;
    /**
     * 左上布局 左 上 3 tableName
     */
    private JTextArea tableName;

    /**
     * 右上 1
     */
    private JLabel serverIPLable;
    private JComboBox<String> serverIP;

    /**
     * 右上 6 保存按钮
     */
    private JButton saveCfgBtn;

    /**
     * 中间
     */
    private JButton crtEntityBtn;

    private JTextArea exeLog;

    private JCheckBox allSelected;
    private JCheckBox invertSelected;

    private JScrollPane fieldSqlScroll;

    @Override
    public void mainUi() {
        int startRow = 1;
        // 先实现单人聊天
        serverIPLable = new JLabel("server ip:");
        UiUtils.setLeftBounds(serverIPLable,startRow);
        panel.add(serverIPLable);
        serverIP = new JComboBox<String>();
        UiUtils.setLeftBounds(serverIP,startRow);
        panel.add(serverIP);

        serverPortLable = new JLabel("server port:");
        UiUtils.setRightBounds(serverPortLable,startRow);
        panel.add(serverPortLable);

        serverPortInput = new JTextField();
        UiUtils.setRightBounds(serverPortInput,startRow);
        panel.add(serverPortInput);
        startRow++;
        // 左右布局  在线用户与 聊天框
        FlatSplitPane splitPanel = new FlatSplitPane();
       // splitPanel.setBounds(0,40,1000,1000);
        UiUtils.setLeftBounds(splitPanel,startRow,2);
        splitPanel.setOrientation(FlatSplitPane.HORIZONTAL_SPLIT);
       // splitPanel.setRightComponent(null);
        FlatSplitPane splitPanel2 = new FlatSplitPane();
        splitPanel2.setOrientation(FlatSplitPane.VERTICAL_SPLIT);
        splitPanel2.setBounds(200,40,800,800);
        splitPanel2.setResizeWeight(0.20);
        splitPanel.setRightComponent(splitPanel2);
        splitPanel.setResizeWeight(0.072);
        panel.add(splitPanel);
    }

    @Override
    public void mainScript() {

    }


    /**
     * 创建实体类
     */
    private void codeGen() {
        // 情况选中表
        ftlNames.clear();
        // 获取选中模板
        ftls.forEach(ftl->{
            logger.info(ftl.getText()+ ftl.isSelected());
            if(ftl.isSelected()){
                ftlNames.add(ftl.getText());
            }
        });


    }

    /**
     * 初始化模板选项
     */
    private void initFtls(){
        // resource 文件
        // FileUtils.resourceFileCopy();
        // 读取模板
        File ftlPath = new File(FreemarkerUtils.TEMPLATE_PATH);
        File [] files = ftlPath.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".ftl");
            }
        });
        ftls = new ArrayList<JCheckBox>(16);
        files = files == null?new File[0]:files;
        for (File file : files) {
            ftls.add(new JCheckBox(file.getName().replace(".ftl","")));
        }

        for (int i = 1; i <= ftls.size(); i++) {
            JCheckBox ftl = ftls.get(i-1);
            UiUtils.setJCheckBoxBounds(ftl,5,1,i+2);
            panel.add(ftl);
        }
    }


    /**
     * 保存配置文件
     *
     * @return
     */
    private boolean saveProperties() {
//        Properties properties = new Properties();
//        // 读取 接口地址 db_driver db_url db_username db_password
//        properties.setProperty("serverPortInput", serverPortInput.getText());
//        properties.setProperty("packageName", packageNameInput.getText());
//        properties.setProperty("tableNames", tableName.getText());
//        properties.setProperty("serverIP", serverIP.getSelectedItem().toString());
//        return PropertyUtils.saveProperties(INIT_PATH_NAME, properties);
        return true;
    }

    /**
     * 初始化配置信息
     */
    @Override
    public void initProperties() {
        //初始化基础参数
        Properties properties = PropertyUtils.readProperties(INIT_PATH_NAME);
        if (properties != null) {
            serverIP.setSelectedItem(properties.getProperty("serverIP"));
            serverPortInput.setText(properties.getProperty("serverPortInput"));
            packageNameInput.setText(properties.getProperty("packageName"));
            tableName.setText(properties.getProperty("tableNames"));
        }else{
            saveProperties();
        }
    }

}
